/*
 * Copyright DimYLiK.
 */

package com.example.myapp.client;

import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;

public class LoadingIndicator {
    private final static Label textLabel = new Label();
    private static PopupPanel indicatorPanel = createPopup();
    private static boolean isVisible = false;

    private static PopupPanel createPopup() {
        HorizontalPanel loadingPanel = new HorizontalPanel();
        loadingPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
        loadingPanel.add(new Image(ImageResources.INSTANCE.loading()));
        loadingPanel.add(textLabel);

        PopupPanel indicatorPanel = new PopupPanel(false, true);
        indicatorPanel.setStyleName("pageLoadingIndicator");
        indicatorPanel.setWidget(loadingPanel);

        indicatorPanel.center();
        return indicatorPanel;
    }

    public static void show(String text) {
        textLabel.setText(text);
        if (!isVisible) {
            isVisible = true;
            indicatorPanel.show();
        }
    }

    public static void hide() {
        if (isVisible) {
            isVisible = false;
            indicatorPanel.hide();
        }
    }
}
